package com.API.dal.providers;

import com.API.dal.entities.*;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Repository
@Scope(value = "request", proxyMode = ScopedProxyMode.INTERFACES)
public class ContractorProviderImp implements ContractorProvider{

    @Autowired
    SessionFactory sessionFactory;


    @Override
    @Transactional(propagation=Propagation.REQUIRED, rollbackFor=Exception.class)
    public void addContractor(Objects contractor) {
        ObjectsTypes ot;
        org.hibernate.classic.Session session  = sessionFactory.getCurrentSession();
        Query query = session.createQuery("from ObjectsTypes ot where ot.name = 'Подрядчик'");
        ot = (ObjectsTypes) query.uniqueResult();
        contractor.setObjectsTypes(ot);
        session.save(contractor);
    }

    @Override
    @Transactional(propagation=Propagation.REQUIRED, rollbackFor=Exception.class)
    public void updateContractor(Objects contractor) {
        Session session = sessionFactory.getCurrentSession();
        session.update(contractor);
    }

    @Override
    @Transactional(readOnly=true,propagation= Propagation.SUPPORTS)
    public List<Objects> getContractors() {
        List<Objects> contractors;
        Session session = sessionFactory.getCurrentSession();
        //id подрядчика (contractor) в БД равен 3
        Query query = session.createQuery("from Objects o where o.objectsTypes.id =  " + 3);
        contractors = query.list();
        return contractors;
    }

    @Override
    @Transactional(readOnly=true,propagation=Propagation.SUPPORTS)
    public List<Objects> getProjectContractors(Objects project) {
        List<Objects> projectContractors;
        Session session = sessionFactory.getCurrentSession();
        Query query = session.createQuery("from Objects o where o.id in " +
                "(from ProjectContractor pc where pc.objectsByProjectId.id = " + project.getId() + ")");
        projectContractors = query.list();
        return projectContractors;
    }

    @Override
    @Transactional(readOnly=true,propagation=Propagation.SUPPORTS)
    public List<Objects> getProjectContractors(int project_id) {
        List<Objects> projectContractors;
        Session session = sessionFactory.getCurrentSession();
        Query query = session.createQuery("from Objects o where o.id in " +
                "(from ProjectContractor pc where pc.objectsByProjectId.id = " + project_id + " )");
        projectContractors = query.list();
        return projectContractors;
    }

    @Override
    @Transactional(readOnly=true,propagation=Propagation.SUPPORTS)
    public List<Objects> getProjectContractors(String project_name) {
        List<Objects> projectContractors;
        Session session = sessionFactory.getCurrentSession();
        Query query = session.createQuery("from Objects o where o.id in " +
                " (from ProjectContractor pc where pc.objectsByProjectId.name = " + project_name + ")");
        projectContractors = query.list();
        return projectContractors;
    }

    @Override
    @Transactional(readOnly=true,propagation=Propagation.SUPPORTS)
    public List<Orders> getContractorOrders(Objects contractor) {
        List<Orders> contractorOrders;
        Session session = sessionFactory.getCurrentSession();
        Query query = session.createQuery("from  Orders o where o.objectsByProviderId.id = " + contractor.getId());
        contractorOrders = query.list();
        return contractorOrders;
    }

    @Override
    @Transactional(readOnly=true,propagation=Propagation.SUPPORTS)
    public List<Orders> getContractorOrders(String contractor_name) {
        List<Orders> contractorOrders;
        Session session = sessionFactory.getCurrentSession();
        Query query = session.createQuery("from  Orders o where o.objectsByProviderId.name = " + contractor_name);
        contractorOrders = query.list();
        return contractorOrders;
    }

    @Override
    @Transactional(readOnly=true,propagation=Propagation.SUPPORTS)
    public List<Orders> getContractorOrders(int contractor_id) {
        List<Orders> contractorOrders;
        Session session = sessionFactory.getCurrentSession();
        Query query = session.createQuery("from  Orders o where o.objectsByProviderId.id = " + contractor_id);
        contractorOrders = query.list();
        return contractorOrders;
    }


}
